/**
 * 
 */
package com.monkeyboy.security.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.social.security.SocialUser;
import org.springframework.social.security.SocialUserDetails;
import org.springframework.social.security.SocialUserDetailsService;
import org.springframework.stereotype.Component;

/**
 * @Description
 *
 * @author Gavin<br>
 *         2019年8月30日
 */
@Component
public class MyUserDetailsService implements UserDetailsService{
	public final static Logger logger = LoggerFactory.getLogger(MyUserDetailsService.class);
	@Autowired
	private PasswordEncoder passwordEncoder;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.security.core.userdetails.UserDetailsService#
	 * loadUserByUsername(java.lang.String)
	 */
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		logger.info("表单登录用户名：{}", username);
		return bindUserByName(username);
	}

	/**
	 * @Description<br>
	 *
	 *                  <pre>
	 *	 {@code
	 *		
	 *	 }
	 *                  </pre>
	 * 
	 * @Author Gavin</br>
	 *         2019年8月30日
	 */
	private SocialUserDetails bindUserById(String userId) {
		logger.info("bindUserById中的userId为：{}", userId);
		// 需要进行数据库的查询，根据用户id或者用户名查询数据库中的数据然后进行比对，这儿写死
		String password = passwordEncoder.encode("123456");

		return new SocialUser(userId, password, true, true, true, true,
				AuthorityUtils.commaSeparatedStringToAuthorityList("admin,ROLE_USER"));
	}

	private UserDetails bindUserByName(String username) {
		logger.info("bindUserByName中的username为：{}", username);
		// 需要进行数据库的查询，根据用户id或者用户名查询数据库中的数据然后进行比对，这儿写死
		String password = passwordEncoder.encode("123456");
		return new User(username, password, true, true, true, true,
				AuthorityUtils.commaSeparatedStringToAuthorityList("admin,ROLE_USER"));
	}
}
